package com.example.mobiletest.component

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.mobiletest.R
import com.example.mobiletest.data.BookingDataManager
import com.example.mobiletest.model.BookingData
import com.example.mobiletest.model.Response
import kotlinx.coroutines.flow.collectLatest

@Composable
fun BookingPage(modifier: Modifier = Modifier, bookingDataManager: BookingDataManager? = null) {

    val context = LocalContext.current
    val manager = bookingDataManager ?: remember {
        BookingDataManager(context)
    }

    var booking by remember { mutableStateOf<Response<BookingData>>(Response.Loading()) }

    LaunchedEffect(Unit) {
        manager.bookingData.collectLatest { response ->
            booking = response
            response.data?.let { data ->
                println(
                    if (data.isCacheData) context.getString(
                        R.string.cache_data,
                        data.toString()
                    ) else context.getString(R.string.network_data, data.toString())
                )
            }
        }
    }

    Column(
        modifier = Modifier
            .fillMaxSize()
            .statusBarsPadding()
            .padding(16.dp)
    ) {
        when (booking) {
            is Response.Loading -> LoadingComponent(modifier)
            is Response.Failure -> FailureComponent(modifier)
            is Response.Success -> SuccessComponent(booking.data, modifier)
        }
    }
}

@Composable
fun LoadingComponent(modifier: Modifier = Modifier) {
    Text(stringResource(R.string.loading), modifier.wrapContentSize())
}

@Composable
fun SuccessComponent(bookData: BookingData?, modifier: Modifier = Modifier) {
    val textModifier = modifier
        .wrapContentSize()
        .padding(start = 6.dp)

    if (bookData != null) {
        ContentWithBookData(bookData, textModifier)
    } else {
        EmptyDataContent(textModifier)
    }
}

@Composable
fun FailureComponent(modifier: Modifier = Modifier) {
    Text(stringResource(R.string.failure), modifier.wrapContentSize())
}

@Composable
private fun ContentWithBookData(bookData: BookingData, modifier: Modifier) {
    Text(
        text = bookData.shipReference,
        fontWeight = FontWeight.Bold,
        fontSize = 24.sp,
        modifier = modifier
    )
    LazyColumn {
        items(bookData.segments, key = { it.id }) { segment ->
            Card(
                shape = RoundedCornerShape(6.dp),
                modifier = Modifier
                    .fillMaxWidth()
                    .height(50.dp)
                    .padding(6.dp)
            ) {
                Text(
                    text = segment.originAndDestinationPair.destinationCity,
                    textAlign = TextAlign.Left,
                    fontSize = 16.sp,
                    modifier = Modifier
                        .fillMaxSize()
                        .padding(start = 6.dp)
                        .wrapContentHeight(Alignment.CenterVertically)
                )
            }
        }
    }
}

@Composable
private fun EmptyDataContent(modifier: Modifier) {
    Text(
        text = stringResource(R.string.empty_data),
        fontWeight = FontWeight.Bold,
        fontSize = 24.sp,
        modifier = modifier
    )
}